package com.qyc.Microservice.controller;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.qyc.Microservice.common.BaseController;
import com.qyc.Microservice.dto.pojo.User;
import com.qyc.Microservice.dto.pojo.User.DetailView;
import com.qyc.Microservice.dto.pojo.User.NameAndAgeView;
import com.qyc.Microservice.dto.pojo.User.SimpleView;
import com.qyc.Microservice.dto.pojo.User.UserId;
import com.qyc.Microservice.dto.result.ResponseResult;
import com.qyc.Microservice.dto.validatedgroup.ValidatedGroup;
import com.qyc.Microservice.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
@Api(tags = "用户")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	@PostMapping("/save")
	public void save(@RequestBody @ApiParam(value = "json/application",required = true) @Validated(value = ValidatedGroup.Save.class) @JsonView(DetailView.class) User user) {
		userService.saveMapper( user);
	}
	
	@PostMapping("/update")
	public int update(@RequestBody @ApiParam(value = "json/application",required = true) @Validated(value = ValidatedGroup.Update.class) @JsonView(DetailView.class) User user) {
	 return	userService.updateMapper(user);
	}
	
	@PostMapping("/delete")
	public int delete(@RequestBody @ApiParam(value = "json/application",required = true) @Validated(value = ValidatedGroup.Delete.class) @JsonView(UserId.class) User user) {
		return userService.deleteMapper(user);
	}
	
	@JsonView(SimpleView.class)
	@GetMapping("/select")
	public User select(@RequestBody @ApiParam(value = "json/application",required = true) @Validated(value =ValidatedGroup.Select.class) @JsonView(SimpleView.class) User user) {
		return userService.selectMapper(user);
	}
	@JsonView(SimpleView.class)
	@GetMapping("/getUserByNameMybatis2")
	@ResponseResult
	public User getUserByNameMybatis2(@NotNull(message = "不能为空！") String name) throws Exception {
		return userService.getUserByNameMybatis2(name);
	}
	
	@JsonView(SimpleView.class)
	@GetMapping("/getUserByNameMybatis3")
	public User getUserByNameMybatis3(@RequestBody @ApiParam(value = "json/application",required = true)  @Validated(value =ValidatedGroup.Select.class) @JsonView(NameAndAgeView.class) User user){
		return userService.getUserByNameMybatis3(user);
	}
}
